//
// Created by Tony on 2024/5/29.
//

#ifndef RT_CODE_CLASS_TABLE_H
#define RT_CODE_CLASS_TABLE_H

#include "Class.h"
#include "NativeMethodTable.h"
#include <cstdint>
#include <unordered_map>

namespace RtCode {

using std::unordered_map;

class ClassTable : public enable_shared_from_this<ClassTable> {
    vector<shared_ptr<Class>> mClassTable;        // index is classId, 0 is invalid
    unordered_map<string, int32_t> mClassIdTable; // key: className, value: classId
    shared_ptr<NativeMethodTable> mNativeMethodTable;

    // status
    bool mIsInited = false;

public:
    ClassTable() = default;

    void init();

    int32_t addClass(const shared_ptr<Class>& clazz);

    int32_t getClassIdByName(const string& className);

    shared_ptr<Class> getClassById(int32_t classId);
    shared_ptr<Class> getClassByName(const string& className);

    shared_ptr<NativeMethodTable> getNativeMethodTable() { return mNativeMethodTable; }

    string toString();
};

} // namespace RtCode

#endif // RT_CODE_CLASS_TABLE_H
